package com.yida.gwj.basis.controller;

import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.basis.domain.*;
import com.yida.gwj.basis.domain.dto.GwjInitBusinessDTO;
import com.yida.gwj.basis.domain.dto.GwjInitBusinessHouseDTO;
import com.yida.gwj.basis.domain.dto.GwjInitBusinessLogDTO;
import com.yida.gwj.basis.domain.request.GwjInitBusinessOwnerRequest;
import com.yida.gwj.basis.domain.request.GwjInitBusinessSaveSubmitRequest;
import com.yida.gwj.basis.service.*;
import com.yida.gwj.common.domain.dto.GwjBusinessFlagDTO;
import com.yida.gwj.common.domain.dto.GwjBusinessStepDTO;
import com.yida.gwj.common.domain.request.GwjBaseBuildRequest;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.utils.GwjConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 小区客体数据更新（基础数据管理模块）Controller
 *
 * @author lqq
 * @date 2022-11-15
 */
@RestController
@RequestMapping("/updateBuild")
public class GwjInitUpdateBuildBusinessController extends BaseController {

	@Autowired
	private IGwjInitBusinessService businessService;

	@Autowired
	private IGwjInitBusinessLogService logService;

	@Autowired
	private IGwjInitUpdateBuildBusinessService updateBuildBusinessService;

	@Autowired
	private IGwjInitBusinessCommunityService communityService;

	@Autowired
	private IGwjInitBusinessBuildService buildService;

	@Autowired
	private IGwjInitBusinessHouseService houseService;

	@Autowired
	private IGwjInitBusinessOwnerService ownerService;

	@Autowired
	private RemoteGwjConfigService remoteGwjConfigService;

	/**
	 * 查询业务列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("gwj:basis:updateBuild:list")
	public TableDataInfo list(@Validated GwjInitBusiness business) {
		startPage();
		business.setBizFlagId(GwjBusinessFlagType.UPDATE_BUILD.getCode());
		List<GwjInitBusiness> list = businessService.getList(business);
		return GwjConvertUtil.convertTable(getDataTable(list), GwjInitBusinessDTO.class);
	}

	/**
	 * 查询业务列表
	 */
	@GetMapping("/info")
	@RequiresPermissions("gwj:basis:updateBuild:list")
	public AjaxResult getBusinessInfo() {
		AjaxResult ajax = AjaxResult.success();
		ajax.put("bizFlag", GwjConvertUtil.convert(remoteGwjConfigService.selectGwjConfBusinessFlagByBizFlagId(GwjBusinessFlagType.UPDATE_BUILD.getCode(), SecurityConstants.INNER).getData(), GwjBusinessFlagDTO.class));
		List<GwjBusinessStepDTO> steps = GwjConvertUtil.convertList(remoteGwjConfigService.selectGwjConfStepListByFlagId(GwjBusinessFlagType.UPDATE_BUILD.getCode(),SecurityConstants.INNER).getData(), GwjBusinessStepDTO.class);
		steps.forEach(stepDTO -> stepDTO.setConfigs(remoteGwjConfigService.selectGwjConfStepConfigInfoCodeListByBizFlagId(stepDTO.getStepId(),SecurityConstants.INNER).getData()));
		ajax.put("steps", steps);
		return ajax;
	}

	/**
	 * 导出业务列表
	 */
	@PostMapping("/export")
	@RequiresPermissions("gwj:basis:updateBuild:export")
	@Log(title = "小区客体数据更新-导出（基础数据管理模块）", businessType = BusinessType.EXPORT, systemName = "gwj")
	public void export(HttpServletResponse response, GwjInitBusiness business) {
		business.setBizFlagId(GwjBusinessFlagType.UPDATE_BUILD.getCode());
		List<GwjInitBusiness> list = businessService.getList(business);
		ExcelUtil<GwjInitBusiness> util = new ExcelUtil<>(GwjInitBusiness.class);
		util.exportExcel(response, list, "业务（基础数据管理模块）数据");
	}

	/**
	 * 送件
	 */
	@PostMapping("/send")
	@RequiresPermissions("gwj:basis:updateBuild:send")
	@Log(title = "小区客体数据更新-送件（基础数据管理模块）", systemName = "gwj")
	public AjaxResult sendBusiness(@Validated(GwjInitBusinessLog.Send.class) @RequestBody GwjInitBusinessLog businessLog) {
		businessService.send(businessLog);
		return AjaxResult.success();
	}

	/**
	 * 回退
	 */
	@DeleteMapping("/fallback")
	@RequiresPermissions("gwj:basis:updateBuild:fallback")
	@Log(title = "小区客体数据更新-回退（基础数据管理模块）", systemName = "gwj")
	public AjaxResult fallback(@Validated(GwjInitBusinessLog.Fallback.class) @RequestBody GwjInitBusinessLog businessLog) {
		businessService.fallback(businessLog);
		return AjaxResult.success();

	}

	/**
	 * 签署意见
	 */
	@PostMapping("/signOpinion")
	@RequiresPermissions("gwj:basis:updateBuild:signOpinion")
	@Log(title = "小区客体数据更新-签署意见（基础数据管理模块）", systemName = "gwj")
	public AjaxResult signOpinion(@Validated(GwjInitBusinessLog.Sign.class) @RequestBody GwjInitBusinessLog businessLog) {
		businessService.signOpinion(businessLog);
		return AjaxResult.success();
	}

	/**
	 * 查看业务流程及业务日志
	 */
	@GetMapping("/flow")
	@RequiresPermissions("gwj:basis:updateBuild:log")
	public AjaxResult flow(Long bizId) {
		return AjaxResult.success(businessService.getBusinessFlow(bizId, GwjBusinessFlagType.UPDATE_BUILD.getCode()));
	}

	/**
	 * 查看业务流程及业务日志
	 */
	@RequiresPermissions("gwj:basis:updateBuild:log")
	@GetMapping("/log/{bizId}")
	public AjaxResult log(@PathVariable Long bizId) {
		return AjaxResult.success(GwjConvertUtil.convertList(logService.getLogList(bizId), GwjInitBusinessLogDTO.class));
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	@RequiresPermissions("gwj:basis:updateBuild:save")
	@Log(title = "小区客体数据更新-保存（基础数据管理模块）", systemName = "gwj")
	public AjaxResult save(@Validated(GwjInitBusinessSaveSubmitRequest.Update.class) @RequestBody GwjInitBusinessSaveSubmitRequest saveSubmitRequest) {
		return AjaxResult.success(updateBuildBusinessService.saveBusiness(saveSubmitRequest));
	}

	/**
	 * 修改保存
	 */
	@PutMapping("/update")
	@RequiresPermissions("gwj:basis:updateBuild:save")
	@Log(title = "小区客体数据更新-修改保存（基础数据管理模块）", systemName = "gwj")
	public AjaxResult update(@Validated(GwjInitBusinessSaveSubmitRequest.Update.class) @RequestBody GwjInitBusinessSaveSubmitRequest saveSubmitRequest) {
		updateBuildBusinessService.updateBusiness(saveSubmitRequest);
		return AjaxResult.success();
	}

	/**
	 * 提交
	 */
	@PostMapping("/submit")
	@RequiresPermissions("gwj:basis:updateBuild:submit")
	@Log(title = "小区客体数据更新-提交（基础数据管理模块）", systemName = "gwj")
	public AjaxResult submit(@Validated @RequestBody GwjInitBusinessSaveSubmitRequest saveSubmitRequest) {
		updateBuildBusinessService.submitBusiness(saveSubmitRequest);
		return AjaxResult.success();
	}

	/**
	 * 案例信息
	 */
	@GetMapping("/caseInfo/{bizId}")
	@RequiresPermissions("gwj:basis:updateBuild:list")
	public AjaxResult caseInfo(@PathVariable Long bizId) {
		return AjaxResult.success(businessService.caseInfo(bizId));
	}

	/**
	 * 小区信息
	 */
	@GetMapping("/communityInfo/{bizId}")
	@RequiresPermissions("gwj:basis:updateBuild:list")
	public AjaxResult communityInfo(@PathVariable Long bizId) {
		AjaxResult result = AjaxResult.success();
		GwjInitBusinessCommunity community = communityService.getOne(bizId);
		if (StringUtils.isNotNull(community)) {
			List<GwjInitBusinessBuild> builds = buildService.getListByBizCommunityId(community.getBizCommunityId());
			result.put("community", community);
			result.put("buildList", builds);
		}
		return result;
	}

	/**
	 * 删页
	 */
	@DeleteMapping("/delete/{bizId}")
	@Log(title = "小区客体数据更新-删页（基础数据管理模块）", systemName = "gwj")
	public AjaxResult deleteBusiness(@PathVariable Long bizId) {
		businessService.deleteByBizId(bizId);
		return AjaxResult.success();
	}

	/**
	 * 房屋维护-房屋信息
	 */
	@GetMapping("/houseInfo")
	@RequiresPermissions("gwj:basis:updateBuild:list")
	public TableDataInfo houseInfo(@Validated GwjInitBusinessHouse businessHouse) {
		startPage();
		List<GwjInitBusinessHouse> list = houseService.getList(businessHouse);
		return GwjConvertUtil.convertTable(getDataTable(list), GwjInitBusinessHouseDTO.class);
	}

	/**
	 * 房屋维护-业主信息
	 */
	@GetMapping("/ownerInfo")
	@RequiresPermissions("gwj:basis:updateBuild:list")
	public AjaxResult ownerInfo(@Validated GwjInitBusinessOwnerRequest request) {
		return AjaxResult.success(ownerService.getByHouseCode(request.getHouseCode(), request.getBizId()));
	}

	/**
	 * 房屋维护-加入选择
	 */
	@GetMapping("/houseConfirm/{bizHouseIds}")
	@Log(title = "小区客体数据更新-房屋维护-加入选择（基础数据管理模块）", systemName = "gwj")
	public AjaxResult confirmSelect(@PathVariable String bizHouseIds) {
		houseService.confirmSelect(bizHouseIds);
		return AjaxResult.success();
	}

	/**
	 * 房屋维护-取消选择
	 */
	@GetMapping("/houseCancel/{bizHouseIds}")
	@Log(title = "小区客体数据更新-房屋维护-取消选择（基础数据管理模块）", systemName = "gwj")
	public AjaxResult cancelSelect(@PathVariable String bizHouseIds) {
		houseService.cancelSelect(bizHouseIds);
		return AjaxResult.success();
	}

	/**
	 * 更新楼栋信息
	 */
	@PutMapping
	public AjaxResult updateBuild(@Validated @RequestBody GwjBaseBuildRequest buildRequest) {
		return AjaxResult.success(updateBuildBusinessService.updateBuild(buildRequest));
	}
}
